package org.mat.ipaas.ucenter.bff.admin.modules.quartz.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.quartz.QuartzJobRestAPI;
import org.mat.ipaas.ucenter.service.dto.quartz.QuartzJobDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 定时任务在线管理
 * @Date: 2019-01-02
 * @Version:V1.0
 */
@RestController
@RequestMapping("/sys/quartzJob")
@Slf4j
@Api(tags = "定时任务接口")
public class QuartzJobController {

    @Autowired
    private QuartzJobRestAPI quartzJobRestAPI;

    /**
     * 分页列表查询
     *
     * @param quartzJobDTO
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MatApiResponse<?> queryPageList(@SpringQueryMap QuartzJobDTO quartzJobDTO,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        MatApiResponse<?> matApiResponse = quartzJobRestAPI.queryPageList(quartzJobDTO, pageNo, pageSize);
        return matApiResponse;
    }

    /**
     * 添加定时任务
     *
     * @param quartzJobDTO
     * @return
     */
    //@RequiresRoles("admin")
    //@RequiresPermissions("system:quartzJob:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MatApiResponse<?> add(@RequestBody QuartzJobDTO quartzJobDTO) {
        MatApiResponse<?> matApiResponse = quartzJobRestAPI.add(quartzJobDTO);
        return matApiResponse;
    }

    /**
     * 更新定时任务
     *
     * @param quartzJobDTO
     * @return
     */
    //@RequiresRoles("admin")
    //@RequiresPermissions("system:quartzJob:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public MatApiResponse<?> eidt(@RequestBody QuartzJobDTO quartzJobDTO) {
        MatApiResponse<?> matApiResponse = quartzJobRestAPI.eidt(quartzJobDTO);
        return matApiResponse;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    //@RequiresRoles("admin")
    //@RequiresPermissions("system:quartzJob:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public MatApiResponse<?> delete(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<?> matApiResponse = quartzJobRestAPI.delete(id);
        return matApiResponse;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    //@RequiresRoles("admin")
    //@RequiresPermissions("system:quartzJob:deleteBatch")
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public MatApiResponse<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        MatApiResponse<?> matApiResponse = quartzJobRestAPI.deleteBatch(ids);
        return matApiResponse;
    }

    /**
     * 暂停定时任务
     *
     * @param id
     * @return
     */
    //@RequiresRoles("admin")
    //@RequiresPermissions("system:quartzJob:pause")
    @GetMapping(value = "/pause")
    @ApiOperation(value = "停止定时任务")
    public MatApiResponse<Object> pauseJob(@RequestParam(name = "id") String id) {
        MatApiResponse<Object> matApiResponse = quartzJobRestAPI.pauseJob(id);
        return matApiResponse;
    }

    /**
     * 启动定时任务
     *
     * @param id
     * @return
     */
    //@RequiresRoles("admin")
    //@RequiresPermissions("system:quartzJob:resume")
    @GetMapping(value = "/resume")
    @ApiOperation(value = "启动定时任务")
    public MatApiResponse<Object> resumeJob(@RequestParam(name = "id") String id) {
        MatApiResponse<Object> matApiResponse = quartzJobRestAPI.resumeJob(id);
        return matApiResponse;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public MatApiResponse<?> queryById(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<?> matApiResponse = quartzJobRestAPI.queryById(id);
        return matApiResponse;
    }

    /**
     * 导出excel
     *
     * @param quartzJobDTO
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(QuartzJobDTO quartzJobDTO) {
        ModelAndView modelAndView = quartzJobRestAPI.exportXls(quartzJobDTO);
        return modelAndView;
    }

    /**
     * 通过excel导入数据
     *
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public MatApiResponse<?> importExcel() throws IOException {
        MatApiResponse<?> matApiResponse = quartzJobRestAPI.importExcel();
        return matApiResponse;
    }

    /**
     * 立即执行
     *
     * @param id
     * @return
     */
    //@RequiresRoles("admin")
    //@RequiresPermissions("system:quartzJob:execute")
    @GetMapping("/execute")
    public MatApiResponse<?> execute(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<?> matApiResponse = quartzJobRestAPI.execute(id);
        return matApiResponse;
    }

}
